package com.opensky.qatest.ui.component;

import org.openqa.selenium.By;

import com.opensky.qatest.ui.page.SalesPage;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * SignIn form on Login page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class LoginPageSignInForm {

    private static final String EMAIL_FIELD = "email";

    private static final String PASSWORD_FIELD = "password";

    private static final String LOGIN_BTN = "//input[@class='button' and @value='Log In']";

    private static final String FORGOTPASS_LINK = "//a[text()='Forgot password?']";

    private static final String NEVERREG_LINK = "//a[text()='Never created a password?']";

    private final WebDriverWrapper driver;

    public LoginPageSignInForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    public boolean isSignInFormElementsPresent() {

	driver.waitAndCheckElementPresent("EMAIL_FIELD", By.id(EMAIL_FIELD));
	driver.waitAndCheckElementPresent("PASSWORD_FIELD", By.id(PASSWORD_FIELD));
	driver.waitAndCheckElementPresent("LOGIN_BTN", By.xpath(LOGIN_BTN));
	driver.waitAndCheckElementPresent("FORGOTPASS_LINK", By.xpath(FORGOTPASS_LINK));
	driver.waitAndCheckElementPresent("NEVERREG_LINK", By.xpath(NEVERREG_LINK));

	return true;
    }

    public SalesPage signIn(String email, String passwd) {
	
	driver.waitAndSendKeys("EMAIL_FIELD", By.id(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.id(PASSWORD_FIELD), passwd);
	driver.waitAndClick("LOGIN_BTN", By.xpath(LOGIN_BTN));
	
	SalesPage salesPage = new SalesPage(driver);
	
	if (salesPage.isPageLoadedNow()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

    public void clickForgotPasswordLink() {

	driver.waitAndClick("FORGOTPASS_LINK", By.xpath(FORGOTPASS_LINK));
    }
}
